package acl.action;

import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import acl.bussiness.UserBusiness;
import acl.hibernate.po.OaUsers;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
	private List userList;
	UserBusiness ubus=new UserBusiness();
	private String start;
	private OaUsers user=null;
	private String errorMsg="";
	public String getErrorMsg() {
		return errorMsg;
	}
	public void setErrorMsg(String errorMsg) {
		this.errorMsg = errorMsg;
	}
	public OaUsers getUser() {
		return user;
	}
	public void setUser(OaUsers user) {
		this.user = user;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	private String limit;
	@JSON(name="list")
	public List getUserList() {
		return userList;
	}
	public void setUserList(List userList) {
		this.userList = userList;
	}
	/*
	 * 前台获取用户列表数据
	 */
	public String list()
	{
		this.userList=ubus.getUserByPage(Integer.parseInt(this.start), Integer.parseInt(this.limit));//ubus.getAllUser();
		
		return Action.SUCCESS;
	}
	/*
	 * 返回到用户编辑页面
	 */
	public String edit()
	{
		//user=ubus.getUserById(1);
		return "edit";
	}
	/*
	 * 保存用户信息，完成后返回编辑页面
	 */
	public String saveedit()
	{
		if(user==null)
		{
			addActionError(getText("SAVE_FAILED"));
			return "save";
		}
		if(user.getUsername().equals(""))
		{
			addActionError(getText("USER_NAMEBLANK"));
			return "save";
		}
		if(ubus.saveOrUpdateUser(this.user)==true)
		{
			addActionMessage(getText("SAVE_SUCCESS"));
			//如果是新建用户，保存成功后，清空原有数据
			if(user.getId()==null||user.getId().equals(0))
				this.user=null;
		}
		else
		{
			addActionError(getText("SAVE_FAILED"));
		}
		
		return "save";
	}
	public String execute() throws Exception
	{
		String curActName=ActionContext.getContext().getActionInvocation().getProxy().getActionName();
		String[] splitnames=curActName.split("_");
		String targetName="";
		if(splitnames.length>0)
			targetName=splitnames[splitnames.length-1];
		else
			targetName=curActName;
		return targetName;
	}
}
